﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Portal.Model;

namespace Portal.Core.Adapters
{
	/// <summary>
	/// Class <see cref="UserAdapter"/> define <see cref="IUser"/> adpater
	/// </summary>
	public class UserAdapter : IUserAdapter
	{
		#region Fields
		private IUser _user = null;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the display name.
		/// </summary>
		/// <value>
		/// The display name.
		/// </value>
		public string DisplayName
		{
			get
			{
				var user = GetUser();
				var fullName = GetFullName(user);
				if (string.IsNullOrWhiteSpace(fullName))
				{
					return user.Login;
				}
				return fullName;
			}
		}

		/// <summary>
		/// Gets the full name.
		/// </summary>
		/// <value>
		/// The full name.
		/// </value>
		public string FullName
		{
			get
			{
				var user = GetUser();
				return GetFullName(user);
			}
		}
		#endregion

		#region Methods
		/// <summary>
		/// Initializes adapter with specified user.
		/// </summary>
		/// <param name="user">The user.</param>
		public void Initialize(IUser user)
		{
			_user = user;
		}

		protected IUser GetUser()
		{
			if (_user == null)
			{
				throw new ArgumentNullException("User is not initialized!");
			}
			return _user;
		}

		private string GetFullName(IUser user)
		{
			if (!string.IsNullOrEmpty(user.FirstName) && !string.IsNullOrEmpty(user.LastName))
			{
				return string.Format("{0} {1}", user.FirstName, user.LastName);
			}
			else if (!string.IsNullOrEmpty(user.FirstName))
			{
				return user.FirstName;
			}
			else if (!string.IsNullOrEmpty(user.LastName))
			{
				return user.LastName;
			}
			return string.Empty;
		}
		#endregion
	}
}
